package container;

import other.CountEntity;
import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomTest {
    @Test
    public void synchronizedAndAtomicInteger() throws InterruptedException {
        CountEntity concurrence = new CountEntity();
        concurrence.setCount(100);
        concurrence.setAtomCount(new AtomicInteger(100));
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                //use synchronized
                synchronized (CountEntity.class) {
                    System.out.println(concurrence.getCount());
                    concurrence.setCount(concurrence.getCount() - 1);
                }
                //use AtomicInteger
                System.out.println("--- " + concurrence.getAtomCount());
                concurrence.setAtomCount(new AtomicInteger(concurrence.getAtomCount().decrementAndGet()));
            }).start();
        }
        Thread.sleep(100);
    }
}
